#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach(const QCameraInfo &cameraInfo, cameras) {
           qDebug() << "CameraInfo:" << cameraInfo.description();
           myCamera = new QCamera(cameraInfo);
       }



    CameraView = new QCameraViewfinder(ui->widget);
    myCamera->setCaptureMode(QCamera::CaptureStillImage);
    myCamera->setViewfinder(CameraView);
    cameraImagecapture = new QCameraImageCapture(myCamera);
    cameraImagecapture->setBufferFormat(QVideoFrame::PixelFormat::Format_Jpeg);
    cameraImagecapture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
    connect(cameraImagecapture,&QCameraImageCapture::imageCaptured,this,&MainWindow::on_capture);
    myCamera->start();
    qDebug()<<myCamera->supportedViewfinderResolutions();
    viewfinderSettings.setResolution(640,480);
    myCamera->setViewfinderSettings(viewfinderSettings);
    CameraView->resize(200,200);
    CameraView->show();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_capture(int id, const QImage &preview)
{
    QString filename;
    filename =QDir::currentPath();
    QImage  im;
    im = preview.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    QPixmap pixmap = QPixmap::fromImage(im);
    ui->label->setPixmap(pixmap);
    qDebug()<<"get capture";
}


void MainWindow::on_screenshot_clicked()
{

    myCamera->searchAndLock();
    cameraImagecapture->capture();
    myCamera->unlock();
    qDebug()<<"jieut";

}
